package com.nb.aspect;

import cn.dev33.satoken.stp.StpUtil;
import com.nb.entity.SysConfig;
import com.nb.entity.SysConfigHistory;
import com.nb.mapper.SysConfigHistoryMapper;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

// com.nb.aspect.SysConfigAspect.java
@Aspect
@Component
public class SysConfigAspect {
    @Autowired
    private SysConfigHistoryMapper historyMapper;


    // 拦截配置更新方法
    @AfterReturning(
            pointcut = "execution(* com.nb.service.impl.SysConfigServiceImpl.update*(..)) && args(sysConfig)",
            returning = "result"
    )
    public void recordUpdateHistory(SysConfig sysConfig, boolean result) {
        if (result) {
            // 构建历史记录
            SysConfigHistory history = new SysConfigHistory();
            history.setConfigId(sysConfig.getId());
            history.setNewKey(sysConfig.getConfigKey());
            history.setNewValue(sysConfig.getConfigValue());
            history.setOperateType("UPDATE");
            history.setOperatedBy(StpUtil.getLoginIdAsString()); // 替换为 StpUtil
            history.setOperatedTime(LocalDateTime.now());
            historyMapper.insert(history);
        }
    }


}